/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLThuVien.BLL;

import QLThuVien.DAL.MyConnectUnit;
import QLThuVien.DTO.*;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Rado
 */
public class BLoaiDocGia {
    MyConnectUnit connect;
    
    public BLoaiDocGia(){
        //this.connect = new MyConnectUnit("localhost", "root", "123456", "QLTV");
        this.connect = new MyConnectUnit("", "sa", "123456", "QLTV");
    }
    
     public HashSet<LoaiDocGia> getLoaiDocGias(String Condition, String OrderBy) throws Exception{
         ResultSet result = this.connect.Select("LOAIDOCGIA", Condition, OrderBy);
         HashSet<LoaiDocGia> loaidocgias = new HashSet<LoaiDocGia>();
         while(result.next()){
             LoaiDocGia loaidocgia = new LoaiDocGia();
             loaidocgia.setMaLoaiDocGia(result.getString("MaLoaiDocGia"));
             loaidocgia.setTenLoaiDocGia(result.getString("TenLoaiDocGia"));
             loaidocgias.add(loaidocgia);
         }
         return loaidocgias;
     }
    
    public HashSet<LoaiDocGia> getLoaiDocGias(String Condition) throws Exception{
        return getLoaiDocGias(Condition, null);
    }
    
    public HashSet<LoaiDocGia> getLoaiDocGias() throws Exception{
        return getLoaiDocGias(null);
    }
    
    public LoaiDocGia getByID(String id) throws Exception{
        HashSet<LoaiDocGia> loaidocgias = this.getLoaiDocGias("MaLoaiDocGia = " + id);
        if(loaidocgias.size() > 0)
            return loaidocgias.toArray(new LoaiDocGia[loaidocgias.size()])[0];
        return null;
    }
    
    public LoaiDocGia getIDByName(String name) throws Exception{
        HashSet<LoaiDocGia> loaidocgias = this.getLoaiDocGias("TenLoaiDocGia = '" + name +"'");
        if(loaidocgias.size() > 0)
            return loaidocgias.toArray(new LoaiDocGia[loaidocgias.size()])[0];
        return null;
    }
    
    public void InsertLoaiDocGia(LoaiDocGia docgia) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("TenLoaiDocGia", docgia.getTenLoaiDocGia());
        this.connect.Insert("LOAIDOCGIA", map);
    }
    
    public void InsertLoaiDocGias(HashSet<LoaiDocGia> loaidocgias) throws Exception{
        for(LoaiDocGia loaidocgia:loaidocgias)
            this.InsertLoaiDocGia(loaidocgia);
    }
    
    public void UpdateLoaiDocGia(LoaiDocGia loaidocgia) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("TenLoaiDocGia", loaidocgia.getTenLoaiDocGia());
        this.connect.Update("LOAIDOCGIA", map, "MaLoaiDocGia = " + loaidocgia.getMaLoaiDocGia());
    }
    
    public void UpdateLoaiDocGias(HashSet<LoaiDocGia> loaidocgias) throws Exception{
        for(LoaiDocGia loaidocgia:loaidocgias)
            this.UpdateLoaiDocGia(loaidocgia);
    }
    
    public void DeleteLoaiDocGia(LoaiDocGia loaidocgia) throws Exception{
        this.connect.Delete("LOAIDOCGIA", "MaLoaiDocGia = " + loaidocgia.getMaLoaiDocGia());
    }
    
    public void DeleteLoaiDocGias(HashSet<LoaiDocGia> loaidocgias) throws Exception{
        for(LoaiDocGia loaidocgia:loaidocgias)
            this.DeleteLoaiDocGia(loaidocgia);
    }
}
